package pw.hais.etgsh.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.squareup.okhttp.Response;

import java.util.List;

import pw.hais.etgsh.R;
import pw.hais.etgsh.app.BaseActivity;
import pw.hais.etgsh.dao.DianTaiDao;
import pw.hais.etgsh.model.DianTaiMp3Model;
import pw.hais.etgsh.model.DianTaiTypeModel;
import pw.hais.etgsh.utils.PlayerMusic;
import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;

/**
 * 儿童电台
 * Created by hais1992 on 2016/1/23/023.
 */
public class DianTaiPlayerActivity extends BaseActivity implements View.OnClickListener {
    public static final String INTENT_ENTITY_KEY = "INTENT_ENTITY_KEY";
    private SeekBar musicProgress;
    private PlayerMusic player;
    private List<DianTaiMp3Model> mp3Models;
    private ImageView image_btn_start, image_btn_sc, image_btn_syq, image_btn_xyq;
    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diantai);

        DianTaiTypeModel dianTaiTypeModel = (DianTaiTypeModel) getIntent().getSerializableExtra(INTENT_ENTITY_KEY);
        if (EmptyUtil.emptyOfObject(dianTaiTypeModel)) {
            L.showShort("发生错误！");
            return;
        }

        musicProgress = (SeekBar) findViewById(R.id.music_progress);
        player = new PlayerMusic(musicProgress);
        image_btn_start = (ImageView) findViewById(R.id.image_btn_start);
        image_btn_sc = (ImageView) findViewById(R.id.image_btn_sc);
        image_btn_syq = (ImageView) findViewById(R.id.image_btn_syq);
        image_btn_xyq = (ImageView) findViewById(R.id.image_btn_xyq);

        getMusicData(dianTaiTypeModel);
    }

    public void getMusicData(DianTaiTypeModel dianTaiTypeModel) {
        loadDialogShow("获取 电台歌曲 中...");
        DianTaiDao.getModelById(dianTaiTypeModel.url, new OnHttpListener<List<DianTaiMp3Model>>() {
            @Override
            public void onSuccess(Response response, List<DianTaiMp3Model> data) {
                loadDialogDismiss();
                mp3Models = data;
                if (!EmptyUtil.emptyOfList(mp3Models)) {
                    playMp3(data.get(index));
                    image_btn_start.setOnClickListener(DianTaiPlayerActivity.this);
                    image_btn_sc.setOnClickListener(DianTaiPlayerActivity.this);
                    image_btn_syq.setOnClickListener(DianTaiPlayerActivity.this);
                    image_btn_xyq.setOnClickListener(DianTaiPlayerActivity.this);
                } else {
                    L.showShort("找不到歌曲！");
                    finish();
                }
            }
        }, new OnHttpListener<String>() {
            @Override
            public void onSuccess(Response response, String data) {
                try {
                    Http.displayImage((ImageView) findViewById(R.id.image_logo),data);
                }catch (Exception e){e.printStackTrace();}
            }
        });
    }


    private void playMp3(final DianTaiMp3Model mp3Model) {
        getSupportActionBar().setTitle(mp3Model.title + "");

        new Thread(new Runnable() {
            @Override
            public void run() {
                player.playUrl(mp3Model.mp3);
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_btn_start:
                if(player.mediaPlayer.isPlaying()){
                    player.pause();
                    image_btn_start.setImageResource(R.drawable.icon_bofang);
                }else{
                    image_btn_start.setImageResource(R.drawable.icon_zanting);
                    player.play();
                }
                break;
            case R.id.image_btn_sc:
                L.showShort("歌曲收藏成功！");
                break;
            case R.id.image_btn_syq:
                if (index <= 0) index = mp3Models.size();
                playMp3(mp3Models.get(--index));
                break;
            case R.id.image_btn_xyq:
                if (index >= mp3Models.size()-1) index = 0;
                playMp3(mp3Models.get(++index));
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.stop();
            player = null;
        }
    }
}
